package util;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.Properties;

public class DBHelper {
    //1+2
    public Connection getCon(){
        Connection con=null;
        try{
            Properties properties = new Properties();
            InputStream is = DBHelper.class.getClassLoader().getResourceAsStream("jdbc.properties");
            properties.load(is);
            Class.forName(properties.getProperty("mysql.driver"));
            String url = properties.getProperty("mysql.url");
            String username = properties.getProperty("mysql.username");
            String password = properties.getProperty("mysql.password");
            con = DriverManager.getConnection(url, username, password);
        } catch (ClassNotFoundException e) {
            //e.printStackTrace();
            System.out.println("mysql 驱动类地址不对，没有导入jar，url路径不对");
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return con;
    }

    //7
    public void closeAll(Connection con, PreparedStatement ps, ResultSet rs){
        if (rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (ps!=null){
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (con!=null){
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    //1-7查询功能汇总，传入3、返回6
    public ArrayList selectinfo(String sql, Class cla, Object...objs){//不确定类型用Object，不确定参数个数用objs
        ArrayList list = new ArrayList();
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try{
            con=getCon();
            ps=con.prepareStatement(sql);
            for (int i = 0; i < objs.length; i++) {
                ps.setObject(i+1,objs[i]);
            }
            rs=ps.executeQuery();
            while (rs.next()){
                //1.根据cla，获得字节码对应类型下的对象；
                Object o = cla.newInstance();
                //2.给对象属性赋值
                Field[] declaredFields = cla.getDeclaredFields();
                for (Field f:declaredFields) {
                    f.setAccessible(true);
                    f.set(o,rs.getObject(f.getName()));
                }
                list.add(o);
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeAll(con,ps,rs);
        }
        return list;
    }

    //求符合条件的数据总个数：
    public int selectinfo1(String sql, Object... objs) {
        int count = 0;
        ArrayList list = new ArrayList();
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            con = getCon();
            ps = con.prepareStatement(sql);
            for (int i = 0; i < objs.length; i++) {
                ps.setObject(i + 1, objs[i]);
            }
            rs = ps.executeQuery();
            if (rs.next()) {
                count = rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(con, ps, rs);
        }
        return count;
    }


    //增删改
    public int updateinfo(String sql,Object...objs){//不确定类型用Object，不确定参数个数用objs
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        int count=0;
        try{
            con=getCon();
            ps=con.prepareStatement(sql);
            for (int i = 0; i < objs.length; i++) {
                ps.setObject(i+1,objs[i]);
            }
            count= ps.executeUpdate();
        }catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeAll(con,ps,rs);
        }
        return count;
    }
}
